package org.yoao.crm.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yoao.crm.DBManager.DBManager;
import org.yoao.crm.bean.CustomerType;

/**
 * @author yxwonline.com
 * @description 
 * @date 2010-7-31 下午01:31:36
 */
public class CustomerTypeDAO extends DBManager {
	private static final Logger log = Logger.getLogger(CustomerTypeDAO.class);
	
	private CustomerType findSingle(Map data) throws Exception{
		CustomerType customerType = null;
		if(data != null && !data.isEmpty()){
			customerType = new CustomerType();
			customerType.setId(Integer.parseInt(data.get("ID").toString()));
			customerType.setTypeCode((String)data.get("TYPECODE"));
			customerType.setTypeName((String)data.get("TYPENAME"));
		}
		return customerType;
	}
	
	public CustomerType findByCode(String typeCode) throws Exception {
		String sql = "select * from customerType where typeCode='"+typeCode+"'";
		return findSingle(querySingleRow(sql));
	}
	
	public CustomerType findById(String id) throws Exception {
		String sql = "select * from customerType where id="+id;
		return findSingle(querySingleRow(sql));
	}
	
	public List findAll() throws Exception{
		String sql = "select ID, TYPECODE, TYPENAME from customerType";
		return find(sql);
	}
	
	private List find(String sql) throws Exception{
		List list = queryMultiColumn(sql);
		List result = null;
		if(list != null && !list.isEmpty()){
			result = new ArrayList();
			for(int i=0;i<list.size();i++){
				Map map = (Map)list.get(i);
				CustomerType customerType = findSingle(map);
				if(customerType == null) customerType = new CustomerType();
				result.add(customerType);
			}
		}
		return result;
	}
	
	public int insert(CustomerType customerType) throws Exception{
		String sql = "insert into customerType(typeCode, typeName)values('"+
					  customerType.getTypeCode()+"', '"+customerType.getTypeName()+"')";
		int result = -1;
		if(update(sql) > -1){
			sql = "select id from customerType where typeCode='"+customerType.getTypeCode()+"'";
			String r = querySingleValue(sql);
			if(r != null && !r.trim().equals("")){
				result = Integer.parseInt(r);
			}
		}
		return result;
	}
	
	public int modify(CustomerType customerType) throws Exception{
		String sql = "update customerType set typeName='"+customerType.getTypeName()+
					 "' where typeCode='"+customerType.getTypeCode()+"'";
		return update(sql);
	}
	
	public int delete(String id) throws Exception{
		String sql = "delete from customerType where id="+id;
		return update(sql);
	}
	
	public synchronized String create(){
		try {
			log.info("开始初始化表customerType...");
			String sql = "SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[customerType]');";
			List list = queryMultiColumn(sql);
			if(list == null){
				log.warn("系统检测失败。\n原因:\n  1.数据库非正常运行;\n  2.帐户没权限不足;\n  3.数据库连接异常。");
				return "系统检测失败。\n原因:\n  1.数据库非正常运行;\n  2.帐户没权限不足;\n  3.数据库连接异常。";
			}
			if(list.isEmpty()){
				log.info("表customerType不存在，开始创建...");
				sql = "/****** 对象:  Table [dbo].[customerType]    脚本日期: 06/24/2009 17:40:16 ******/ " +
					  "SET ANSI_NULLS ON;" +
					  "SET QUOTED_IDENTIFIER ON;" +
					  "SET ANSI_PADDING ON;" +
					  "CREATE TABLE [dbo].[customerType](" +
					  		    "[id] [int] IDENTITY(1,1) NOT NULL," +
					  			"[typeCode] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL," +
					  			"[typeName] [varchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL," +					  							  			
					  "CONSTRAINT [PK_customerType] PRIMARY KEY CLUSTERED" +
					  "(" +
					  			"[id] ASC" +
					  ")WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]" +
					  ") ON [PRIMARY];" +
					  "SET ANSI_PADDING OFF;";
				
				if(this.update(sql) < 0){
					log.warn("表customerType初始化失败!");
					return "表customerType初始化失败!";
				}
				log.info("表customerType创建成功!");
				return "表customerType创建成功!";
			}
			else{
				log.info("表customerType已存在!");
				return "表customerType已存在!";
			}
		} 
		catch (Exception e) {
			log.warn("表customerType初始化失败!");
			e.printStackTrace();
			return "表customerType初始化失败!" + e.toString();
		}
	}
}